﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Todo.Entity.Models;

namespace Todo.Logic.Server
{
   public class ThingServer
    {
        private TodoContext _ctx = new TodoContext();
        List<Inventory> _inventorys = new List<Inventory>();
        List<Things> _things = new List<Things>();

        ///// <summary>
        ///// 判断是否有重复的事件ID
        ///// </summary>
        ///// <param name="thingId"></param>
        ///// <returns></returns>
        //public bool JudgeThingIdRepeat(int thingId)
        //{
        //    var result = _ctx.Things.SingleOrDefault(item => item.Id == thingId);
        //    return result == null ? true : false;
        //}

        /// <summary>
        /// 添加事项
        /// </summary>
        /// <param name="inventory"></param>
        /// <param name="thingId"></param>
        /// <param name="toDoListContent"></param>
        /// <param name="CompletionStatus"></param>
        /// <param name="expirationDate"></param>
        /// <param name="remark"></param>
        public void AddThing(int userId, int inventoryId,
            string toDoListContent,DateTime expirationDate,
            string remark,string name)
        {
            _things = _ctx.Things.Where(item => item.InventoryId == inventoryId && item.UserId==userId).ToList();
            Things thing = new Things
            {
                Name = name,
                CreateTime = DateTime.Now,
                ThingContent = toDoListContent,
                CompleteStatue = false,
                IsPastdue = false,
                OverTime = expirationDate,
                Remark = remark,
                UserId = userId,
                InventoryId= inventoryId,
            };
            _things.Add(thing);
            _ctx.Add(thing);
            _ctx.SaveChanges();
        }

        /// <summary>
        /// 获取一个事件
        /// </summary>
        /// <param name="inventory"></param>
        /// <param name="thingId"></param>
        /// <returns></returns>
        public Things GetThing(int userId,int thingId)
        {
            return _ctx.Things.SingleOrDefault(thing => thing.Id == thingId&&
            thing.UserId==userId);
        }

        /// <summary>
        /// 删除事项
        /// </summary>
        /// <param name="thing"></param>
        /// <param name="inventory"></param>
        /// <returns></returns>
        public void RemoveThing(Things thing)
        {
            _ctx.Things.Remove(thing);
            _ctx.SaveChanges();
        }

        /// <summary>
        /// 添加备注
        /// </summary>
        /// <param name="thing"></param>
        /// <param name="remark"></param>
        public void AddRemark(Things thing,string remark)
        {
            thing.Remark = remark;
            _ctx.SaveChanges();
        }

        /// <summary>
        /// 添加过期时间
        /// </summary>
        /// <param name="thing"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        public void AddExpirationDate(Things thing,DateTime time)
        {
            thing.OverTime = time;
            _ctx.SaveChanges();
        }

        /// <summary>
        /// 改变事件状态
        /// </summary>
        /// <param name="thing"></param>
        /// <param name="thingState"></param>
        /// <returns></returns>
        public void ChangeState(Things thing,bool thingState)
        {
            thing.CompleteStatue = thingState;
            _ctx.SaveChanges();
        }

        public bool IsBelongToUser(int userId,int thingId)
        {
            return _ctx.Things.Any(item => item.UserId == userId && item.Id == thingId) ? true : false;
        }


        /// <summary>
        /// 获取所有事项
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public List<Things> GetViewAllThing(int userId)
        {
            return _ctx.Things.Where(item => item.UserId == userId).ToList();
        }

        /// <summary>
        /// 统计未完成事项
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int CountNotCompelet(int userId)
        {
            return _ctx.Things.Where(item => item.UserId == userId && item.CompleteStatue == false).Count();
        }

        /// <summary>
        /// 获取所有过期事项
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public List<Things> GetOverTimeThings(int userId)
        {
            _things = GetViewAllThing(userId);
            foreach (var thing in _things)
            {
                OverTime(thing);
            }
            _things = _ctx.Things.Where(item => item.UserId == userId && item.IsPastdue == true).ToList();
            return _things;
        }

        /// <summary>
        /// 将过期事项改为过期状态
        /// </summary>
        /// <param name="thing"></param>
        public void OverTime(Things thing)
        {
            thing.IsPastdue = thing.OverTime <= DateTime.Now;
            _ctx.SaveChanges();
        }

    }
}
